﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using LPTask.Interfaces.Views;

namespace LPTask.UI
{
    internal class GeneralView : IGeneralView
    {
        public event EventHandler ActiveTaskChangeNeeded;
        public event EventHandler SolveTaskNeeded;

        private readonly FormMain _formMain;

        public bool Enabled
        {
            get { return _formMain.Enabled; }
            set { _formMain.Enabled = value; }
        }

        public string SelectedTaskName
        {
            get { return _formMain.SelectedTaskName; }
        }

        public GeneralView()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            _formMain = new FormMain();
            _formMain.ActiveTaskChangeNeeded += FormMain_ActiveTaskChangeNeeded;
            _formMain.SolveTaskNeeded += FormMain_SolveTaskNeeded;
        }
        
        public IUserMessageView UserMessageView
        {
            get { return WinFormsViewManager.Instance.UserMessageView; }
        }

        public void ShowView()
        {
            Application.Run(_formMain);
        }

        public void SetTaskNames(List<string> taskNames)
        {
            _formMain.SetTaskNames(taskNames);
        }

        public void SetActiveTaskView(ITaskView taskView)
        {
            _formMain.SetActiveTaskView(taskView);
        }

        private void OnSolveTaskNeeded()
        {
            if (SolveTaskNeeded != null)
            {
                SolveTaskNeeded(this, EventArgs.Empty);
            }
        }

        private void OnActiveTaskChangeNeeded()
        {
            if (ActiveTaskChangeNeeded != null)
            {
                ActiveTaskChangeNeeded(this, EventArgs.Empty);
            }
        }

        private void FormMain_SolveTaskNeeded(object sender, EventArgs e)
        {
            OnSolveTaskNeeded();
        }

        private void FormMain_ActiveTaskChangeNeeded(object sender, EventArgs e)
        {
            OnActiveTaskChangeNeeded();
        }
    }
}